// JavaScript Document
jQuery.fn.validateRegForm=function(){
    var sUserName = $("#username").val();
    var sPassword = $("#password").val();
    var sPassword2 = $("#password2").val() ;
    if ((sUserName =="") || (sUserName=="Your email")){
     $().showError("Please input your Email!");
     return false ;
    }
    if ((sPassword =="") || (sPassword=="Set password")){
     $().showError("Please input password");
     return false ;
    }
    if ((sPassword2 =="") || (sPassword2=="Password again")){
        $().showError("Please input password");
        return false ;
       }
    if (sPassword != sPassword2) {
    	$().showError("Passwords do not match!");
        return false ;
    }
};

jQuery.fn.userRegister=function(){
	if($().validateRegForm()==false){
		return;
	}
	var username=$("#username").val();
	var password=$("#password").val();
	var password2=$("#password2").val();
	var nickname=$("#nickname").val();
	var data={
		username:username,
		password:password,
		password2:password2,
		nickname:nickname,
	};
	$.ajax({
		type:"post",
		async:true,
		url:"RegisterServlet",
		data:data,
		success:function(result){
			if(result!=null||result!=""){
				if(result=="success"){
					$(location).attr("href","eventlist.jsp");
				}else{
					$().showError(result);
				}
			}
		}
	});
};

jQuery.fn.validateLoginForm=function(){
    var sUserName = $("#loginUsername").val();
    var sPassword = $("#loginPassword").val() ;
    if ((sUserName =="") || (sUserName=="Your email")){
     $().showError("Please input your Email");
     return false ;
    }
    if ((sPassword =="") || (sPassword=="Your password")){
     $().showError("Please input password");
     return false ;
    }
   };
   
jQuery.fn.userLogin=function(){
	if($().validateLoginForm()==false){
		return;
	}
	var username=$("#loginUsername").val();
	var password=$("#loginPassword").val();
	var data={
		username:username,
		password:password,
	};
	$.ajax({
		type:"post",
		async:true,
		url:"LoginServlet",
		data:data,
		success:function(result){
			if(result!=null||result!=""){
				if(result=="success"){
					$(location).attr("href","eventlist.jsp");
				}else{
					$().showError(result);
				}
			}
		}
	});
};

jQuery.fn.linkCreateEvent=function(){
	$(location).attr("href","eventCreate.jsp");
};

jQuery.fn.importContact=function(){
	$("#importButton2").attr("disable","disable");
	var gmail=$("#gmailAddress").val();
	var password=$("#gmailPassword").val();
	var userMail=$("#userMail").val();
	var data={
		userMail:userMail,
		gmail:gmail,
		password:password,
	};
	$.ajax({
		type:"post",
		async:true,
		url:"importContact",
		data:data,
		success:function(result){
			if(result!=null||result!=""){
				if(result=="success"){
					$().showImportMsg("Import Successes.");
				}else{
					$().showImportError(result);
				}
			}
			$("#importButton2").removeAttr("disable");
		}
	});
};

jQuery.fn.showMessage=function(msg){
	$("#messageDiv").html(msg);
	$("#messageDiv").hide();
	$("#messageDiv").fadeIn(1000);
	$("#messageDiv").fadeOut(3000);
};

jQuery.fn.showError=function(error){
	$("#errorMsgDiv").html(error);
	$("#errorMsgDiv").show();
	$("#errorMsgDiv").fadeOut(6000);
};

jQuery.fn.showImportMsg=function(msg){
	$("#importMessageDiv").html(msg);
	$("#importMessageDiv").hide();
	$("#importMessageDiv").fadeIn(1000);
	$("#importMessageDiv").fadeOut(3000);
};

jQuery.fn.showImportError=function(error){
	$("#importErrorMsgDiv").html(error);
	$("#importErrorMsgDiv").show();
	$("#importErrorMsgDiv").fadeOut(6000);
};

$(document).ready(function() {
	$("#registerButton").click(function(){
		$().userRegister();
	});
	$("#loginButton").click(function(){
		$().userLogin();
	});
	$("#createButton").click(function(){
		$().linkCreateEvent();
	});
	$("#importButton").click(function(){
		$("#importForm").fadeIn();
	});
	$("#importButton2").click(function(){
		$().importContact();
	});
	$("#gmailAddress").focus(function(){
		var gmail=$("#gmailAddress").val();
		if(gmail=="Gmail Address"){
			$("#gmailAddress").val("");
		}
	});
	//$().showMeta(metadata);
});
